package com.qf.bbapi.controller;

import com.aliyun.credentials.http.HttpRequest;
import com.qf.bbapi.config.SystemConfig;
import com.qf.bbapi.service.intf.ShopCarService;
import com.qf.bbapi.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * @author wodexinhuai
 * @create 2022-07-02-20:42
 */

@RestController
@RequestMapping("/api/shopcar/")
@Api(tags = "添加购物车相关操作")
@CrossOrigin
public class ShopCarController {
    @Resource
    private ShopCarService service;

    @PostMapping("addGoods.do")
    @ApiOperation("添加商品及数量到购物车")
    public R addGoods(HttpServletRequest request, @RequestParam(value = "goodsId") Long goodsId, @RequestParam(value = "goodsNum") Integer goodsNum){
        System.out.println("goodsId = " + goodsId);
        return service.addGoods(request.getIntHeader(SystemConfig.TOKEN_HEADER),goodsId,goodsNum);
    }

    @GetMapping("addOne.do")
    @ApiOperation("点击数量+1")
    public R addOne(HttpServletRequest request, long goodsId){
        return service.addOne(request.getIntHeader(SystemConfig.TOKEN_HEADER),goodsId);
    }

    @GetMapping("minusOne.do")
    @ApiOperation("点击数量-1")
    public R minusOne(HttpServletRequest request, long goodsId){
        return service.minusOne(request.getIntHeader(SystemConfig.TOKEN_HEADER),goodsId);
    }

    @PostMapping("delGoods.do")
    @ApiOperation("删除购物车信息")
    public R delGoods(@RequestBody List<Long> ids){
        return service.delGoods(ids);
    }

    @GetMapping("shopCarInfo.do")
    @ApiOperation("展示某个用户所有购物车信息")
    public R shopCarInfo(HttpServletRequest request){
        return service.shopCarInfo(request.getIntHeader(SystemConfig.TOKEN_HEADER));
    }

    @PostMapping("update.do")
    @ApiOperation("更新数量")
    public R update(@RequestParam(value = "id") Long id,@RequestParam(value = "goodsNum") Integer goodsNum){
        return service.update(id,goodsNum);
    }

    @PostMapping("showPart.do")
    @ApiOperation("展示某个用户部分购物车信息")
    public R showPart(@RequestBody List<Long> ids){
        return service.showPart(ids);
    }
}
